<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui"
          name="viewport">
    <title>菩提阁</title>
    <link href="./../images/favico.ico" rel="icon">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link href="../../backend/plugins/element-ui/index.css" rel="stylesheet"/>
    <!--引入vant样式-->
    <link href="../styles/vant.min.css" rel="stylesheet"/>
    <!-- 引入样式  -->
    <link href="../styles/index.css" rel="stylesheet"/>
    <!--本页面内容的样式-->
    <link href="./../styles/login.css" rel="stylesheet"/>
</head>
<body>
<div id="login" v-loading="loading">
    <div class="divHead">登录</div>
    <div class="divContainer">
        <el-input maxlength='20' placeholder=" 请输入手机号码" v-model="form.phone"/>
        </el-input>
        <div class="divSplit"></div>
        <el-input maxlength='20' placeholder=" 请输入验证码" v-model="form.code"/>
        </el-input>
        <span @click='getCode'>{{sendCode}}</span>
    </div>
    <div class="divMsg" v-if="msgFlag">手机号输入不正确，请重新输入</div>
    <el-button :class="{btnSubmit:1===1,btnNoPhone:!form.phone,btnPhone:form.phone}" @click="btnLogin" type="primary">
        登录
    </el-button>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant样式 -->
<script src="./../js/vant.min.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/login.js"></script>
</body>
<script>
    new Vue({
        el: "#login",
        data() {
            return {
                sendCode: "获取验证码",
                // 倒计时时间
                time_count: 60,
                timer: null,
                // 控制发送验证码按钮
                codebtn: true,
                form: {
                    phone: '',
                    code: ''
                },
                msgFlag: false,
                loading: false
            }
        },
        computed: {},
        created() {
        },
        mounted() {
        },
        methods: {
            getCode() {
                let that = this;
                if (this.codebtn) {
                    this.form.code = ''
                    const regex = /^(16[0-9]{9})|(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;
                    if (regex.test(this.form.phone)) {
                        this.msgFlag = false
                        // this.form.code = (Math.random()*1000000).toFixed(0)
                        let phone = this.form.phone
                        console.log("手机号为:", phone)
                        let code = this.form.code
                        console.log("验证码为:", code)
                        // sessionStorage.setItem("code", code)

                        if (!that.timer) {
                            that.timer = setInterval(() => {
                                if (that.time_count > 0) {
                                    that.time_count--;
                                    that.sendCode = "重新发送" + that.time_count + "s";
                                } else {
                                    that.sendCode = "获取验证码";
                                    clearInterval(that.timer);
                                    that.timer = null;
                                    that.time_count = 60;
                                    this.codebtn = true;
                                }
                            }, 1000);
                            this.codebtn = false;
                            sendMsgApi({phone: this.form.phone})
                            this.$notify({type: 'success', message: '短信已发送'});
                        }
                    } else {
                        this.msgFlag = true
                    }
                } else {
                    this.$notify({type: 'warning', message: '请勿重复发送短信'});
                }
            },
            async btnLogin() {
                if (this.form.phone && this.form.code) {
                    this.loading = true
                    const res = await loginApi(this.form)
                    this.loading = false
                    if (res.code === 1) {
                        sessionStorage.setItem("userPhone", this.form.phone)
                        window.requestAnimationFrame(() => {
                            window.location.href = '/front/index.html'
                        })
                    } else {
                        this.$notify({type: 'warning', message: res.msg});
                    }
                } else {
                    this.$notify({type: 'warning', message: '请输入手机号码'});
                }
            }
        }
    })
</script>
</html>